﻿@model IEnumerable<LanDBom.Models.Formation>
@using LanDBom.Resources

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Genertals.cshtml";
}

<h2>ERP物料信息</h2>
<form class="row padtop col-12" action="#" method="post">
    <fieldset class="boun">
        <legend class="len">@Resource.String155</legend>
        <div class="row">
            <p class="col">
                @Html.Label(Resource.String240)
                @Html.TextBox("shen")
            </p>
            <p class="col">
                @Html.Label(Resource.String241)
                @Html.TextBox("ming")
            </p>
            <p class="col">
                @Html.Label(Resource.String104)
                @Html.TextBox("shi")
            </p>
            <p class="col" style="display:none">
                @Html.Label(Resource.String104)
                <select id="WUliao" name="WUliao">
                    <option value="">---请选择---</option>
                    <option value="WL">报价系统物料</option>
                    <option value="ERP">ERP传递物料</option>
                </select>
            </p>
            <p class="col-2">
                <input type="button" value="@Resource.String155" name="cha" class=" btn-secondary btn-sm subsed" />
            </p>
        </div>
    </fieldset>
</form>
<div><button style="display:none" id="qwgypz">前往工艺配置</button></div>
<p>
    <button class="btn" id="ERPBB">ERP物料报表</button>
    @if (MyAuthentication.IsLogin())
    {
        if (MyAuthentication.GetUsers().DE_zhiji > 1)
        {
            <button class="btn" id="ERpdijia" >物料价格上传至ERP</button>
            <button class="btn" id="ERPsc">ERP物料删除</button>
            <button class="btn" id="ERPgx">物料标准成本更新</button>
        }
    }

</p>
<div>
    <table class="tab table">
        <thead class="thead-light">
            <tr style="text-align-last:center;">
                <th><input type="checkbox" id="cbkall" /></th>
                <th>
                    @Html.Label(Resource.String244)
                </th>
                <th>
                    @Html.Label(Resource.String245)
                </th>
                <th>
                    @Html.Label("规格")
                </th>
                <th>
                    @Html.Label("表面处理")
                </th>
                <th>
                    @Html.Label("原材料")
                </th>
                <th>
                    @Html.Label("创建日期")
                </th>
                <th>
                    @Html.Label("一级分类")
                </th>
                <th>
                    @Html.Label("二级分类")
                </th>
                <th>
                    @Html.Label("价格")
                </th>
                <th>
                    @Html.Label("ERP采购单价")
                </th>
                <th>
                    @Html.Label("ERP采购含税单价(13%)")
                </th>
                <th>
                    @Html.Label("所在单据")
                </th>
                <th>
                    @Html.Label("创建人")
                </th>
                <th>
                    @Html.Label("供应商")
                </th>
                <th></th>
            </tr>
        </thead>

        <tbody class="gun" id="FORMHJ" style="height:550px;">
          
        </tbody>
    </table>
    <div style="display: flex; justify-content: space-between;">
        <span>共<span id="Hangs"></span> 行</span>
        <nav aria-label="Page navigation example" class="row" style="margin-right:50px;">
            <ul class="pagination">
            </ul>
            共<span id="komdr"></span>页
        </nav>
    </div>
    <div id="WT_dh_mb2" style="display: none; position: absolute; border: 1px black solid; left: 25%; top: 33%; background-color: white; background-color: #f1f1f1;height:200px;width:300px">
        <div style="text-align: right; background-color: #84acde;"><span class="WT_qxbh2" style=" margin: 20px;"><i class="bi bi-x-square"></i></span></div>
        <div style="margin: 20px 10px; padding: 10px; background-color: #fff;">
            <div id="WJ_XZLJ"></div>
        </div>
        <div style="text-align:center"><button class="WT_qxbh2">@Resource.String126</button></div>
    </div>
</div>
<div style="z-index: 300; position: fixed; width: 100%; height: 100%;background-color:#bacee9; left: 0px; top: 0px; display: none; opacity: 0.5; " id="Pinzh"></div>



@section scripts{
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        $(function () {
            var hsunKM=0
            Hnaghshu(hsunKM)

            $(document).on("click", "#cbkall", function () {
                if ($("#cbkall").prop("checked") == true) {
                    console.log(1)
                    $(".cbks").prop("checked", true)
                }
                else {
                    $(".cbks").prop("checked", false)
                }
            })
            $(document).on("click", "#ERpdijia", function () {
                $("#Pinzh").show()
                CG_XX=[]
                $("input[class=cbks]:checked").parent().each(function (i, item) {
                    CG_XX.push({
                        FM_bai: $(item).next().text()
                    })
                    console.log("共" + CG_XX.length + "数据");
                    console.log(CG_XX);
                })
                if (CG_XX.length == 0) {
                    if (confirm("是否全部同步至ERP,全部同步执行时间过长")) {
                        $.ajax({
                            url: "/Formations/Cderp",
                            type: 'POST',
                            data: { "state": 1 },
                            success: function (text) {
                                $("#Pinzh").hide()
                                alert(text)
                            },
                            error: function (text) {
                                $("#Pinzh").hide()
                                alert("系统错误");
                            }
                        })
                    }
                } else {
                    $.ajax({
                        url: "/Formations/DGwlSC",
                        type: 'POST',
                        data: { "Bian": CG_XX },
                        success: function (text) {
                            $("#Pinzh").hide()
                            alert(text)
                        },
                        error: function (text) {
                            alert("系统错误");
                        }
                    })
                }
               
            })
           
            $(document).on("click", "#ERPgx", function () {
                if (confirm("是否更新物料")) {
                    $("#Pinzh").show()
                    $.ajax({
                        url: "/Application/Bom_Plgx",
                        type: 'POST',
                        data: { "id": 1 },
                        success: function (text) {
                            alert(text)
                            $.ajax({
                                url: "/Formations/Cderp",
                                type: 'POST',
                                data: { "state": 1 },
                                success: function (text) {
                                    $("#Pinzh").hide()
                                    alert(text)
                                },
                                error: function (text) {
                                    $("#Pinzh").hide()
                                    alert("系统错误");
                                }
                            })
                        },
                        error: function (text) {
                            $("#Pinzh").hide()
                            alert("系统错误");
                        }
                    })
                }
            })
            $(document).on("click", "#ERPsc", function () {
                CG_XX=[]
                $("input[class=cbks]:checked").parent().each(function (i, item) {
                    CG_XX.push({
                        FM_bai: $(item).next().text()
                    })
                    console.log("共" + CG_XX.length + "数据");
                    console.log(CG_XX);
                })
                $.ajax({
                    url: "/Formations/delectfoer",
                    type: 'POST',
                    data: { "formation": CG_XX },
                    success: function (text) {
                        alert("成功删除" + text + "条物料")
                        Hnaghshu()
                    },
                    error: function (text) {
                        alert("系统错误");
                    }
                })
            })
            $(document).on("click", "#qwgypz", function () {
                var index = 0
                var temp=0
                $("input[class=cbks]:checked").each(function (e) {
                    index++
                    var lh = $(e.target).next().text()
                    if (lh.includes("WL")) {
                        temp++
                    }
                })
                if (index == 0) {
                    alert("请先选择需要配置的物料")
                }
                else {
                    if (temp != 0) {
                        alert("所选物料里有非erp物料，请检查所选物料")
                    }
                    else {
                        Tboms()
                    }
                }
            })
            function Tboms() {
                var BOMinise = [];
                var shen ;
                var hu = 0;
                $("input[class=cbks]:checked").each(function (index, obj) {
                    if ($("#labes", obj).html() == "Leve0") {
                        hu = hu + 1 * 1000;
                    }
                    if ($("#labes", obj).html() == "leve1") {
                        hu = hu + "-A";
                    }
                    var Sone = $("#leibie", obj).val() == null ? $("#leibie", obj).val() : $("#leibie", obj).val();
                    console.log(Sone)
                    var Sted = $("#leib_ji", obj).val() == null ? $("#leib_ji", obj).val() : $("#leib_ji", obj).val();
                    Sone = Sone == "纸质" ? "1" : Sone == "木质" ? "2" : Sone == "塑料" ? "3" : Sone == "金属制品" ? "4" : Sone == "布" ? "5" : Sone == "胶粘制品" ? "6" : Sone == "其他" ? "7" : Sone == "套件" ? "8" : Sone;
                    BOMinise.push({
                        BOM2_chen: $("#labes", obj).html(),//层级
                        BOM2_wuLiu: $("input[name='empName'] ", obj).val(),//物料信息
                        BOM2_liao: $("input[name='empLiao'] ", obj).val(),//料号
                        BOM2_Guige: $("input[name='empMiao'] ", obj).val(),//产品规格
                        BOM2_Nian: $("input[name='empNian'] ", obj).val(),//年用量
                        BOM2_fenzi: $("input[name='CL_name']", obj).val(), //计量单位
                        BOM2_Yong: $("input[name='empYong'] ", obj).val(),//Bom 用量
                        BOM2_Lei: $("input[name='CL_Gong']", obj).val(),/*自制或采购*/
                        BOM2_bian: shen,
                        BOM2_Ctime: $("#txtqishi").val(),
                        BOM2_BQing: hu,
                        BOM2_Zhuang: 1,
                        BOM2_Sone: Sone,
                        BOM2_ST: Sted
                        //预留添加标识
                    })
                    console.log("共" + BOMinise.length + "数据");
                    console.log(BOMinise);
                })

                //发出请求
                $.ajax({
                    url: "/Application/AddAppli",
                    type: "POST",
                    data: JSON.stringify(BOMinise),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (text) {
                        if (text > 0) {
                            alert("添加成功")
                        } else {
                            alert("添加失败" + text.msg);
                        }
                    },
                    error: function (text) {
                        alert("系统错误" + text);
                    }
                })
            }
            function Hnaghshu(TNT) {
                var BB_SQ_xmmc = $("#shen").val()
                var BB_SQ_khmc = $("#ming").val()
                var BB_SQ_sqrq = $("#shi").val()
                var WUliao = "ERP"
                $.ajax({
                    url: '/Formations/Index',
                    type: 'POST',
                    data: { "shen": BB_SQ_xmmc, "ming": BB_SQ_khmc, "shi": BB_SQ_sqrq, "shujina": TNT, "state": 1, "WUliao": WUliao },
                    success: function (date) {
                        console.log(date)
                        var HJ = date * 1
                        var HNH = HJ / 10 < 1 ? 1 : Math.ceil(HJ / 10) 
                        $("#Hangs").text(HJ)
                        $("#komdr").text(HNH)
                        fenye(TNT)
                        $.ajax({
                            url: '/Formations/Index',
                            type: 'POST',
                            data: { "shen": BB_SQ_xmmc, "ming": BB_SQ_khmc, "shi": BB_SQ_sqrq, "WUliao": WUliao, "shujina": TNT-1, "state": 2 },
                            success: function (date1) {
                                console.log(date1)
                                HDJHF(date1)
                            }, error: function () {
                                alert("系统错误")
                            }
                        })
                    }, error: function () {
                        alert("系统错误")
                    }
                })
            }
            $("#li19").css({ "color": "black", "background-color": "white" })

            function fenye(a) {
                a = a == 0 ? "1" : a
                var hjin = a * 1
                var shunddd = $("#komdr").text() * 1
                $(".pagination li").remove()
                var $ul = ""
                $ul += (`<li class="page-item"><span class="page-link shiyong"><<<</span></li>`)
                for (var i = 1; i <= shunddd; i++) {
                    if (shunddd < 4) {
                        if (i < 5) {
                            if (hjin == i) {
                                $ul += "<li class='page-item'><span class='page-link shiyong' id='shiyongll' style='background-color:#c4ded9'>" + i + "</span></li>";
                            } else {
                                $ul += "<li class='page-item'><span class='page-link shiyong'>" + i + "</span></li>";
                            }
                        } else {
                            $ul += "<li class='page-item'><span class='page-link'>.....</span></li>";
                            break
                        }
                    }
                    if (shunddd >= 4) {
                        if (i < (hjin - 2) || i >= (hjin + 2)) {
                            continue
                        }
                        if (i > (hjin - 2) && i < (hjin + 2)) {
                            if (hjin == i) {
                                $ul += "<li class='page-item'><span class='page-link shiyong' id='shiyongll' style='background-color:#c4ded9'>" + i + "</span></li>";
                            } else {
                                $ul += "<li class='page-item'><span class='page-link shiyong'>" + i + "</span></li>";
                            }
                        } else {
                            $ul += "<li class='page-item'><span class='page-link'>.....</span></li>";
                        }
                    }
                }
                $ul += (`<li class="page-item"><span class="page-link shiyong">>>></span></li>`);
                $(".pagination").append($ul)
            }
            $(document).on("click", ".shiyong,.subsed", function () {
                var sjind = $(this).text()
                var shunddd = $("#komdr").text()
                sjind = sjind == "<<<" ? 1 : sjind == ">>>" ? shunddd : sjind * 1
                Hnaghshu(sjind)
            })
            function HDJHF(e) {
                $("#FORMHJ tr").remove()
                $.each(e, function (i, item) {
                    var Dhun = item.FM_price == null ? "价格未提交,到单据中提交" : item.FM_price;
                    var Dan = item.FM_long == null ? "为找到对应价格" : item.FM_long;
                    var Han = item.FM_kuan == null ? "为找到对应价格" : item.FM_kuan;
                    $tr = $(`<tr>
                                <td><input type="checkbox" class="cbks"/></td>
                                <td>${item.FM_bai}</td>
                                <td>${ item.FM_name}</td>
                                <td>${item.FM_gg}</ td >
                                <td>${item.FM_bmcl}</td>
                                <td>${item.FM_ycl}</td>
                                <td>${item.FM_zx}</td>
                                <td>${item.FM_yj}</td>
                                <td>${item.FM_ej}</td>
                                <td> ${Dhun}</td>
                                <td> ${Dan}</td>
                                <td> ${Han}</td>
                                <td> ${item.FM_kong}</td>
                                <td> ${item.FM_ms}</td>
                                <td>${item.PR_name}</td>
                                @if (MyAuthentication.IsLogin())
                                {
                                    if (MyAuthentication.GetUsers().DE_zhiji >1|| MyAuthentication.GetUsers().DE_bumeng ==3)
                                    {
                                    <td><a class="ERPzhi">传递至ERP</a>|<a href="/Formations/Delete/${item.FM_ID}" style="color:red">删除</a>
                                    </td>
                                    }
                                    else
                                    {
                                    <td>权限不足</td>
                                    }
                                }</tr>`)
                    $("#FORMHJ").append($tr)
                })
            }

            $(document).on("click", "#ERPBB", function () {
                $.ajax({
                    url: '/Formations/DownloadExcel',
                    type: 'POST',
                    data: { "state": 1 },
                    success: function (date) {
                        var text = date
                        $("#WT_dh_mb2").css({ "display": "block" })
                        $("#WJ_XZLJ").append($(`<a href="/Contracts/Download_file?path=${text}" id="log_SZ" class="WT_qxbh2">下载</a>`))
                    }, error: function () {
                        alert("系统错误")
                    }
                })
            })
            $(document).on("click", ".WT_qxbh2", function () {
                $("#WT_dh_mb2").css({ "display": "none" })
                $("#WJ_XZLJ a").remove()
            })
        })
    </script>
}